#include "pm/workrequest.h"

using namespace pm;

const wxString WorkRequest::TABLE_NAME   = wxT("work_requests");

WorkRequest::WorkRequest()
: db::Table()
, amount(0)
{
}

WorkRequest::WorkRequest(wxSQLite3ResultSet& res)
: db::Table(res)
, name(      res, wxT("name"))
, amount(    res, wxT("amount"))
, date(      res, wxT("date"))
, id_project(res, wxT("id_project"), db::ID::undefined)
{
}

db::update_value_t WorkRequest::prepare_insert(wxSQLite3Database& db)
{
	if (id_project.valid() == false) {
		return db::update_value_t(false, wxSQLite3Statement());
	}
	
	wxSQLite3Statement stmt = db.PrepareStatement("insert into work_requests values(null, ?, ?, ?, ?)");
	stmt.Bind(1, name());
	stmt.Bind(2, amount());
	stmt.BindDate(3, date());
	stmt.Bind(4, id_project());
	return db::update_value_t(true, stmt);
}

db::update_value_t WorkRequest::prepare_update(wxSQLite3Database& db)
{
	wxSQLite3Statement stmt = db.PrepareStatement("update work_requests set name = ?, amount = ?, date = ?, id_project = ? where id = ?");
	stmt.Bind(1, name());
	stmt.Bind(2, amount());
	stmt.BindDate(3, date());
	stmt.Bind(4, id_project());
	stmt.Bind(5, id());
	return db::update_value_t(true, stmt);
}


GetWorkRequestsByProject::GetWorkRequestsByProject(const db::ID& idProject)
: id_project(idProject)
{
}

wxSQLite3Statement GetWorkRequestsByProject::prepare(wxSQLite3Database& db)
{
	wxSQLite3Statement stmt = db.PrepareStatement("select id, name, amount, date, id_project from work_requests where id_project = ?");
	stmt.Bind(1, id_project());
	return stmt;
}
